<?php

class Application_Model_DbTable_Article extends Zend_Db_Table_Abstract
{

    protected $_name = 'txy_article';
    
    public function getArticleById($id){
    	$id=(int)$id;
    	$row=$this->fetchRow('article_id = '.$id);
    	if(!$row){
    		throw new Exception("没有对应的文章");
    	}
    	return $row;
    }

    public function addNewArticle($data){
    	$key_words=$data['article_key_words'];
    	$key_words_array=explode(',', $key_words);
    	$keyWordsModel=new Application_Model_DbTable_Keywords();
    	$this->_db->beginTransaction();
    	try{
	    	$this->insert($data);
	    	$article_id=$this->_db->lastInsertId();
	    	foreach ($key_words_array as $key_word){
	    		$keyWordsModel->addKeyWords(array('item_id'=>$article_id,'key_words'=>$key_word,'item_type'=>'0'));
	    	}
	    	$this->_db->commit();
    	} catch (Exception $e){
    		$this->_db->rollBack();
    		$e->getMessage();
    	}
    	
    }
    
    public function updateCount($id,$count){
    	$data=array('article_count_num'=>$count);
    	$this->update($data, 'article_id='.$id);
    }
    //查询index页面的文章
    public function getIndexArticleList($article_content_type,$limit=20){
    	$select = new Zend_Db_Table_Select($this);
    	$select->from(array('t'=>'txy_article'),array('t.article_id','t.article_url_spell','t.article_title'))
    		   ->where('t.article_content_type='.$article_content_type)
    		   ->order('t.article_create_date desc')
    		   ->limit($limit);  	
    	$articleList=$this->fetchAll($select);
    	return $articleList;
    }
    //点击最多的文章
    public function getHotArticle($limit){
    	$select=new Zend_Db_Table_Select($this);
    	$select->from(array('t'=>'txy_article'),array('t.article_id','t.article_url_spell','t.article_title'))
    		   ->order('article_count_num desc')
    		   ->limit($limit);
    	$res=$this->fetchAll($select);
    	return $res->toArray();
    }
    //按文章分类查询
    public function getArticleList($article_content_type,$limit=20){
    	$select = new Zend_Db_Table_Select($this);
    	$select->from(array('t'=>'txy_article'),array('t.article_id','t.article_url_spell','t.article_title'))
    		   ->where('t.article_content_type='.$article_content_type)
    		   ->limit($limit);  	
    	$articleList=$this->fetchAll($select);
    	return $articleList->toArray();
    }
    //获取文章名长度>10(30)的文章
    public function getNewestLongTitleArticle($limit=8){
        $select = new Zend_Db_Table_Select($this);
    	$select->from('txy_article',array('article_id','article_url_spell','article_title','article_create_date','article_content_type'))
    		   ->where(new Zend_Db_Expr("LENGTH(article_title)>30"))->order('article_id desc')
    		   ->limit($limit);  	
    	$articleList=$this->fetchAll($select);
    	return $articleList;
    }
    //获取文章名长度>10(30)的文章
    public function getNewestShortTitleArticle($limit=8){
        $select = new Zend_Db_Table_Select($this);
    	$select->from('txy_article',array('article_id','article_url_spell','article_title','article_create_date'))
    		   ->where(new Zend_Db_Expr("LENGTH(article_title)<=30"))->order('article_id desc')
    		   ->limit($limit);  	
    	$articleList=$this->fetchAll($select);
    	return $articleList;
    }
    //更新文章
    public function updateArticleById($data,$id){
    	$this->update($data, 'article_id='.$id);
    }

}

